﻿using System;
using System.Drawing;
using System.Windows.Forms;
using JetBrains.Annotations;
using Kaleida.ServiceMonitor.Framework;
using Kaleida.ServiceMonitor.Model.Runtime;

namespace Kaleida.ServiceMonitor.UI
{
    public partial class PollFrequencyControl : UserControl
    {
        private bool focused;
        private Duration value = CompiledScript.DefaultPollFrequency;

        public event EventHandler<PollFrequencyValueChangingEventArgs> ValueChanging;
        public event EventHandler ValueChanged;

        public PollFrequencyControl()
        {
            InitializeComponent();
            HideEditLabel();
        }

        public new bool Enabled
        {
            get { return textBox.Enabled; }
            set { textBox.Enabled = value; }
        }

        [NotNull]
        internal Duration Value
        {
            get { return value; }
            set
            {
                if (value == null) throw new ArgumentNullException("value");

                this.value = value;
                UpdateDisplayFromValue();
            }
        }

        private void textBox_Leave(object sender, EventArgs e)
        {
            var text = textBox.Text;
            if (text.IsDuration())
            {
                var newValue = text.ToDuration();
                if (ValueChanging != null)
                {
                    var eventArgs = new PollFrequencyValueChangingEventArgs(value, newValue);
                    ValueChanging(this, eventArgs);
                    newValue = eventArgs.NewValue;
                }
                value = newValue;

                if (ValueChanged != null)
                {
                    ValueChanged(this, EventArgs.Empty);
                }
            }
            UpdateDisplayFromValue();

            HideEditLabel();
            focused = false;
        }

        private void UpdateDisplayFromValue()
        {
            textBox.Text = value.ToString();
            lblEditIndicator.Text = "";
        }

        private void textBox_Enter(object sender, EventArgs e)
        {
            lblEditIndicator.Visible = true;
            lblEditIndicator.Text = "(editing)";
            Width = textBox.Width + lblEditIndicator.Width;
            textBox.Width = Width - lblEditIndicator.Width;
            lblEditIndicator.Location = new Point(textBox.Width, 0);

            if (MouseButtons == MouseButtons.None)
            {
                textBox.SelectAll();
                focused = true;
            }
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void HideEditLabel()
        {
            lblEditIndicator.Visible = false;
            Width = textBox.Width;
            textBox.Width = Width;
        }

        private void textBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (!focused)
            {
                if (textBox.SelectionLength == 0)
                    textBox.SelectAll();
                focused = true;
            }
        }
    }
}
